import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: hp
 * Date: 2024-07-07
 * Time: 16:53
 */
public class Test {
    public static void main(String[] args) throws SQLException {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入id： ");
        int id = scanner.nextInt();
        System.out.print("请输入姓名： ");
        String name = scanner.next();

        DataSource dataSource = new MysqlDataSource();//向上转型
        ((MysqlDataSource) dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java112?characterEncoding=utf8&useSSL=false");//向下转型
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("071711");

        Connection connection = dataSource.getConnection();

        //System.out.println(connection);

        //String sql = "insert into student2 values("+ id +",'"+name+"')";
        String sql = "insert into student2 values(?,?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1,id);
        preparedStatement.setString(2,name);

        int n = preparedStatement.executeUpdate();
        System.out.println(n);


        preparedStatement.close();
        connection.close();




    }
}
